<template>
	<div ref="mgrDialog" class="dialog-wrap">
		  
		<el-dialog 
			:title="headerTitle" 
			width="40%"
			@close="resetDialog" 
			:visible.sync="DialogVisible"
		>
			
			<el-row :gutter="20">

				<el-form 
					v-if = "editForm != null"
					ref="dialogForm"
					:rules="rules"
					size="mini"
					:label-width="formLabelWidth"
					:model.sync="editForm">
					
					<el-col :span="22">
						<el-form-item label="农事序号" prop="farmCode">
		      				<el-input v-model="editForm.farmCode" autocomplete="off" placeholder="请输入农事序号"></el-input>
		    			</el-form-item>
		    		</el-col>
		    		<el-col :span="22">
						<el-form-item label="农事名称" prop="farmName">
		      				<el-input v-model="editForm.farmName" autocomplete="off" placeholder="请输入农事名称"></el-input>
		    			</el-form-item>
		    		</el-col>
				</el-form>
			</el-row>
			
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" type="primary" @click="confirm">确定</el-button>
			    <el-button size="mini" type="danger" @click="cancel">取消</el-button>
			</div>	
		</el-dialog>

    
	</div>
</template>


<script>
import { getRolesTree,getRoleTreeList } from "@/api/admin/roles"
import { getUserInfo,alterAvatar } from "@/api/user"
import { convertDataToTree } from "@/utils/util"
import InputTree from "@/components/InputTree"
import { baseUrl } from '@/config/env';
//上传图片
const uploadUrl = baseUrl+'/rest/common/upload';
import { getToken } from '@/utils/auth'
export default{
	props:{
		//新增还是编辑
	    type:{
	      type:[Number,String],
	      defalut:1
	    },
		visible:{
			type:Boolean,
			default:false
		},
		form:{
			type:Object,
			default:null
		}
	},
	components:{
		InputTree
  	},
	watch:{
		form(newVal,oldVal){
			if(newVal != null){
				this.editForm = newVal;
			}else{
				//this.editForm = this.defaultForm;
				this.editForm.farmName = ''
				this.editForm.farmCode = ''
			}	
		},
		type(newVal,oldVal){
	      	if(newVal == 1){
	        	this.headerTitle = "新增农事信息"
	      	}else if(newVal == 2){
	        	this.headerTitle = "修改农事信息"
	      	}
	    },
	    visible(val){
	    	this.DialogVisible = val;
	    	if(this.form === null){
	    		this.editForm = this.defaultForm;
	    	}
	    }
	},
	data(){
		return{
			baseUrl:baseUrl,
			uploadUrl:uploadUrl,
			uploadHeaders:{'Authorization':'Bearer ' + getToken()},
			productTypeList:['原材料','半成品','制成品'],
			addPic:require("@/assets/farm/images/add-pic_08.jpg"),
			headerTitle:"新增农事信息",
			DialogVisible:false,
			defaultForm:{
				name:'',
				chargePerson:'',
				phone:'',
				crop:'',
				lot:'',
				address:'',
				pic:'',
			
			},
			roleTree:[],
			testEquipmentGroup:[
				{
					checked: true, 
					iconSkin: null, 
					id: 0, 
					name: "根目录", 
					open: true, 
					pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 1, 
				 	name: "继电器设备", 
				 	open: true, 
				 	pId: 0
				},
				{
					checked: null, 
				 	iconSkin: null, 
				 	id: 2, 
				 	name: "采集设备", 
				 	open: true, 
				 	pId: 0
				}
			],
			roleTreeProps:{
        		children: 'childrenNodes',
        		label: 'name',
        		value: 'id'
      		},
			editForm:{
				
			},
			formLabelWidth:'100px',
			//验证规则
		    rules:{
		        cropName:[{required:true,message:"请输入农场序号",trigger:"blur"}],
		        cropNo:[{required:true,message:"请输入农场名称",trigger:"blur"}],
		    },
		}
	},
	mounted(){
		this.getRoleTree();
	},
	methods:{
		//获取树形菜单
		getRoleTree(){

			getRoleTreeList().then((res)=>{
				console.log("-----------获取树形菜单-----------")
				console.log(res);
				//this.roleTree = convertDataToTree(res.data);
				this.roleTree = convertDataToTree(this.testEquipmentGroup)
			})

		},
		resetDialog(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		//上级角色选择
		roleSelect(data){
			this.editForm.pid = data.id;
		},
		//取消按钮
		cancel(){
			this.DialogVisible = false;
			this.editForm = null;
			this.$emit('cancel')
		},
		confirm(){
			this.$refs.dialogForm.validate((valid)=>{
		        if (valid) {
		          this.DialogVisible = false;

		          if(this.editForm.pid === 0){
		          	this.editForm.pid = '0';
		          }

		          this.$emit('confirm',{type:this.type,data:this.editForm});

		        } else {
		           
		            return false;
		        }

      		})
		}

	}
}


</script>

<style scoped>
	.with-full{
	    width:100%!important;
	}
</style>